#include "Bootstrap.h"
#include <stdio.h>

BootstrapImplement::BootstrapImplement() 
{

}

BootstrapImplement::~BootstrapImplement() 
{

}

int BootstrapImplement::GetBundleID() 
{
	return 0;
}

ModuleContext* BootstrapImplement::GetContext() 
{
	//return &s_ModuleContextImplement;
	return 0;
}

const char* BootstrapImplement::GetSymbolicName()
{
	return "osgi.framework";
}

const Version* BootstrapImplement::GetVersion()
{
	static Version s_FrameworkVersion = {1,0,0};
	return &s_FrameworkVersion;
}

int BootstrapImplement::GetState()
{
	return m_State;
}

void BootstrapImplement::Init()
{
	if( m_State==Module::STARTING || m_State==Module::ACTIVE || m_State==Module::STOPPING)
		return;

	printf( "OSGi framework start to initial......\n" );
	Install();

	// Register any framework services. For example, PackageAdmin, ConditionalPermissionAdmin, StartLevel.
	// TODO: regist all framework service first

	
	m_State = Module::STARTING;   
	printf( "OSGi framework start to initialed.\n" );
}

void BootstrapImplement::Install()
{
    return;
}

void BootstrapImplement::Start(int options)
{
	if( m_State!=Module::STARTING )
		Init();

	printf( "OSGi framework start to start......\n" );

	m_State = Module::ACTIVE;
	_SyncNotifyEvent( FrameworkEvent::STARTED, this );
	printf( "OSGi framework start to started.\n" );
}

void BootstrapImplement::Stop(int options)
{
	printf( "OSGi framework start to stop......\n" );
	// The method returns immediately to the caller after initiating the following steps to be taken on another thread.
	m_State = Module::STOPPING;

	// Unregister all services registered by this Framework.

	// Event handling is disabled.

	m_State = Module::RESOLVED;

	// All resources held by this Framework are released. This includes threads, bundle class loaders, open files, etc.

	// Notify all threads that are waiting at waitForStop that the stop operation has completed.

	//After being stopped, this Framework may be discarded, initialized or started.
	printf( "OSGi framework start is stopped.\n" );
}

void BootstrapImplement::Update()
{
	Stop(0);
	Start(0);
}

void BootstrapImplement::Uninstall()
{

}

FrameworkEvent BootstrapImplement::WaitForStop( long timeout )
{
	FrameworkEvent r = {FrameworkEvent::WAIT_TIMEOUT, 0};
	if( m_State!=Module::STARTING && m_State!=Module::ACTIVE && m_State!=Module::STOPPING) {
		r.m_Type = FrameworkEvent::WARRING;
		return r;
	}

	printf( "OSGi framework wait for stop......\n" );

	for( long runtime = 0; runtime<timeout; runtime += 0/*calculate delta time.*/ ){
		if( Module::ACTIVE!=m_State ) {
			r.m_Type = FrameworkEvent::STOPPED;
			break;
		}
		_MainLoop();
	}	

	printf( "OSGi framework stopped.\n" );
    /**
    A Framework Event indicating the reason this method returned. The following FrameworkEvent types may be returned by this method.
    STOPPED - This Framework has been stopped.
    STOPPED_UPDATE - This Framework has been updated which has shutdown and will now restart.
    STOPPED_BOOTCLASSPATH_MODIFIED - This Framework has been stopped and a bootclasspath extension bundle has been installed or updated. The VM must be restarted in order for the changed boot class path to take affect.
    ERROR - The Framework encountered an error while shutting down or an error has occurred which forced the framework to shutdown.
    WAIT_TIMEDOUT - This method has timed out and returned before this Framework has stopped.
	*/	
	return r;
}

void BootstrapImplement::_SyncNotifyEvent(int event, Bootstrap* bs)
{

}
